<?php 


class mycrypt {  
 
    public $pubkey;  
    public $privkey;  
 
    function __construct() {  
        $this->pubkey = file_get_contents('key/rsa_public_key.pem');  
        $this->privkey = file_get_contents('key/pkcs8_rsa_private_key.pem');  
    }  
 

    //使用私钥加密算法
    public function encrypt($data) { 

        if (openssl_private_encrypt($data, $encrypted, $this->privkey))  
            $data = base64_encode($encrypted);  
        else  
            throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');  
 
        return $data;  
    }  
 

    //使用公钥解密算法
    public function decrypt($data) {  

        if (openssl_public_decrypt(base64_decode($data), $decrypted, $this->pubkey))  
            $data = $decrypted;  
        else  
            $data = '';  
 
        return $data;  
    }  
 
}  


